package com.balance.anole.client.retail.dialog;

import java.util.List;

import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.balance.anole.business.to.ShopReceiptTO;
import com.balance.anole.client.retail.provider.HoldSelectProvider;
import com.balance.anole.client.retail.service.RetailService;
import com.balance.anole.erp.util.ServicesCenter;

public class RetailHoldSelectDialog extends Dialog{

	final RetailService rs = ServicesCenter.getServicesCenter().getService(RetailService.class);

	private String agentId;

	private ShopReceiptTO shopReceiptTO;

	public ShopReceiptTO getShopReceiptTO() {
		return shopReceiptTO;
	}

	public void setShopReceiptTO(ShopReceiptTO shopReceiptTO) {
		this.shopReceiptTO = shopReceiptTO;
	}

	public RetailHoldSelectDialog(Shell parentShell,String agentId) {
		super(parentShell);
		this.agentId = agentId;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("处理挂起");
	}

	protected Control createDialogArea(final Composite parent) {
		Composite area = (Composite)super.createDialogArea(parent);
		area.setLayout(new FormLayout());
		final GridData gd_comp = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd_comp.heightHint = 272;
		gd_comp.widthHint = 540;
		area.setLayoutData(gd_comp);
		Composite top = new Composite(area, SWT.NONE);
		final FormData fd_top = new FormData();
		fd_top.right = new FormAttachment(100, -5);
		fd_top.bottom = new FormAttachment(0, 20);
		fd_top.top = new FormAttachment(0, 0);
		fd_top.left = new FormAttachment(0, 0);
		top.setLayoutData(fd_top);
		top.setLayout(new FormLayout());

		final Composite composite_1 = new Composite(area, SWT.NONE);
		final FormData fd_composite_1 = new FormData();
		fd_composite_1.right = new FormAttachment(100, -5);
		fd_composite_1.left = new FormAttachment(top, 0, SWT.LEFT);
		fd_composite_1.top = new FormAttachment(top, 5, SWT.BOTTOM);
		fd_composite_1.bottom = new FormAttachment(100, -5);
		composite_1.setLayoutData(fd_composite_1);
		composite_1.setLayout(new FormLayout());

		List<ShopReceiptTO> l_heldReceipt = rs.getListOfHeldReceipt(agentId);

		final WritableList wl = new WritableList(l_heldReceipt,ShopReceiptTO.class);

		TableViewer tableViewer = new TableViewer(composite_1, SWT.BORDER|SWT.FULL_SELECTION);
		createColumns(tableViewer, new String[]{"交易编号","销售员","交易时间","货物数量","交易柜台"}, new int[] {75, 75,150,75,75});

		ObservableListContentProvider opContentProvider = new ObservableListContentProvider();
		tableViewer.setContentProvider(opContentProvider);
		tableViewer.setLabelProvider(new HoldSelectProvider());
		tableViewer.setInput(wl);
		//default to select the first one.
		if(wl.size() > 0){
			tableViewer.getTable().select(0);
		}

		final Table table = tableViewer.getTable();
		final FormData fd_table = new FormData();
		fd_table.bottom = new FormAttachment(100, -5);
		fd_table.right = new FormAttachment(100, -5);
		fd_table.left = new FormAttachment(0, 5);
		fd_table.top = new FormAttachment(0, 5);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		tableViewer.addDoubleClickListener(new IDoubleClickListener() {

			public void doubleClick(DoubleClickEvent event) {
				StructuredSelection selection = (StructuredSelection)event.getSelection();
				Object element = selection.getFirstElement();
				if(element != null){
					ShopReceiptTO shopReceiptTO = (ShopReceiptTO)element;
					setShopReceiptTO(shopReceiptTO);
				}
				close();
			}
		});

		return area;
	}

	protected void createColumns(final TableViewer viewer, String[] titles, int[] bounds) {
		for (int i = 0; i < titles.length; i++) {
			TableViewerColumn viewerColumn  = new TableViewerColumn(viewer, SWT.NONE);
			final TableColumn column = viewerColumn.getColumn();
			column.setText(titles[i]);
			column.setWidth(bounds[i]);
			column.setResizable(true);
			column.setMoveable(true);
		}
	}

	protected Control createButtonBar(Composite parent) {
		return parent;
	}

}
